rm(list=ls(all=TRUE))

library(foreign)
library(car)
library(stargazer)

#===================SE==================#
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  library(plyr)
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x) }
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=T),
                     mean = mean   (xx[[col]], na.rm=T),
                     sd   = sd     (xx[[col]], na.rm=T)
                   ) },
                 measurevar )
  datac <- rename(datac, c("mean"= measurevar))
  datac$se <- datac$sd / sqrt(datac$N)
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  return(datac)}

#===============2022====================#

dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/TNSS 2022/1111209.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)

tw2022=NULL
tw2022$ind_even_attack=recode(dataset$Q7,"1=-2;2=-1;3=1;4=2;else=0")
tw2022$ind_no_attack=recode(dataset$Q8,"1=-2;2=-1;3=1;4=2;else=0")
tw2022=data.frame(tw2022)



tw2022$uni_even_diff=recode(dataset$Q9,"1=-2;2=-1;3=1;4=2;else=0")
tw2022$iuni_no_diff=recode(dataset$Q10,"1=-2;2=-1;3=1;4=2;else=0")


tw2022$chn_att=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2022$usa_def=dataset$Q14  #new answer 1=army 2=weapon 3=others 4 =no help
tw2022$ppl_def=recode(dataset$Q20,"1=-2;2=-1;3=1;4=2;else=0") #new q if US will not come
tw2022$army=recode(dataset$Q16,"1=1;2=-1;else=0")

tw2022$business=NA
tw2022$eco_more=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2022$eco_for_poli=recode(dataset$Q3,"1=-2;2=-1;3=1;4=2;else=0")
tw2022$home_eco_crisis=NA
tw2022$tw_eco_crisis=NA

tw2022$us_score=NA
tw2022$china_score=NA
tw2022$japan_score=NA

tw2022$tundo6=recode(dataset$Q5,"1=-2;2=2;3=-1;4=1;else=0")

tw2022$TaiwanID=recode(dataset$Q28,"1=1;2=0;3=-1;else=0")
tw2022$age=111-dataset$Q29
tw2022$age[tw2022$age<20]=NA
tw2022$edu=recode(dataset$Q30,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2022$fathershengi=recode(dataset$Q31,"3=1;else=0")
tw2022$male=recode(dataset$Q55,"1=1;else=0")
tw2022$KMT=recode(dataset$partyid,"1=1;else=0")
tw2022$DPP=recode(dataset$partyid,"2=1;else=0")

tw2022$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2022$GREEN=recode(dataset$partyid,"2=1;5=1;20=1;21=1;23=1;36=1;else=0")
tw2022$TSU=recode(dataset$partyid,"36=1;else=0")
tw2022$PFP=recode(dataset$partyid,"5=1;else=0")
tw2022$NEW=recode(dataset$partyid,"3=1;else=0")
tw2022$NPP=recode(dataset$partyid,"21=1;else=0")
tw2022$TPP=recode(dataset$partyid,"38=1;else=0")

tw2022$year=2022
tw2022$taipei=NA
tw2022$region=dataset$AREAR

summary(tw2022)



#===============2020====================#

dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/TNSS2020/PP2097A1.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)

tw2020=NULL
tw2020$ind_even_attack=recode(dataset$Q6,"1=-2;2=-1;3=1;4=2;else=0")
tw2020$ind_no_attack=recode(dataset$Q7,"1=-2;2=-1;3=1;4=2;else=0")
tw2020=data.frame(tw2020)



tw2020$uni_even_diff=recode(dataset$Q8,"1=-2;2=-1;3=1;4=2;else=0")
tw2020$iuni_no_diff=recode(dataset$Q9,"1=-2;2=-1;3=1;4=2;else=0")


tw2020$chn_att=recode(dataset$Q12,"1=-2;2=-1;3=1;4=2;else=0")
tw2020$usa_def=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2020$ppl_def=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")
tw2020$army=recode(dataset$Q11,"1=1;2=-1;else=0")

tw2020$business=NA
tw2020$eco_more=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2020$eco_for_poli=recode(dataset$Q3,"1=-2;2=-1;3=1;4=2;else=0")
tw2020$home_eco_crisis=NA
tw2020$tw_eco_crisis=recode(dataset$Q1,"1=1;2=-1;else=0")

tw2020$us_score=NA
tw2020$china_score=NA
tw2020$japan_score=NA

tw2020$tundo6=recode(dataset$Q4,"1=-2;2=2;3=-1;4=1;else=0")

tw2020$TaiwanID=recode(dataset$Q27,"1=1;2=0;3=-1;else=0")
tw2020$age=109-dataset$Q28
tw2020$age[tw2020$age<20]=NA
tw2020$edu=recode(dataset$Q29,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2020$fathershengi=recode(dataset$Q30,"3=1;else=0")
tw2020$male=recode(dataset$Q56,"1=1;else=0")
tw2020$KMT=recode(dataset$partyid,"1=1;else=0")
tw2020$DPP=recode(dataset$partyid,"2=1;else=0")

tw2020$BLUE=recode(dataset$partyid2,"1=1;3=1;5=1;22=1;29=1;30=1;else=0")
tw2020$GREEN=recode(dataset$partyid2,"2=1;4=1;6=1;21=1;23=1;24=1;else=0")
tw2020$TSU=recode(dataset$partyid2,"6=1;else=0")
tw2020$PFP=recode(dataset$partyid2,"5=1;else=0")
tw2020$NEW=recode(dataset$partyid2,"3=1;else=0")
tw2020$NPP=recode(dataset$partyid2,"21=1;else=0")
tw2020$TPP=recode(dataset$partyid2,"39=1;else=0")

tw2020$year=2020
tw2020$taipei=NA
tw2020$region=dataset$AREAR

summary(tw2020)



#===========================2019==========================
dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2019_TNSS/PP199711.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)

tw2019=NULL
tw2019$ind_even_attack=recode(dataset$Q8,"1=-2;2=-1;3=1;4=2;else=0")
tw2019$ind_no_attack=recode(dataset$Q9,"1=-2;2=-1;3=1;4=2;else=0")
tw2019=data.frame(tw2019)



tw2019$uni_even_diff=recode(dataset$Q10,"1=-2;2=-1;3=1;4=2;else=0")
tw2019$iuni_no_diff=recode(dataset$Q11,"1=-2;2=-1;3=1;4=2;else=0")


tw2019$chn_att=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2019$usa_def=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")
tw2019$ppl_def=recode(dataset$Q19,"1=-2;2=-1;3=1;4=2;else=0")
tw2019$army=recode(dataset$Q15,"1=1;2=-1;else=0")

tw2019$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2019$eco_more=recode(dataset$Q3,"1=1;2=-1;else=0")
tw2019$eco_for_poli=recode(dataset$Q4,"1=-2;2=-1;3=1;4=2;else=0")
tw2019$home_eco_crisis=recode(dataset$Q5,"1=1;2=-1;else=0")
tw2019$tw_eco_crisis=NA

tw2019$us_score=NA
tw2019$china_score=NA
tw2019$japan_score=NA

tw2019$tundo6=recode(dataset$Q6,"1=-2;2=2;3=-1;4=1;else=0")

tw2019$TaiwanID=recode(dataset$Q29,"1=1;2=0;3=-1;else=0")
tw2019$age=108-dataset$Q30
tw2019$age[tw2019$age<20]=NA
tw2019$edu=recode(dataset$Q31,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2019$fathershengi=recode(dataset$Q32,"3=1;else=0")
tw2019$male=recode(dataset$Q58,"1=1;else=0")
tw2019$KMT=recode(dataset$partyid,"1=1;else=0")
tw2019$DPP=recode(dataset$partyid,"2=1;else=0")

tw2019$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2019$GREEN=recode(dataset$partyid,"2=1;5=1;6=1;else=0")
tw2019$TSU=recode(dataset$partyid,"5=1;else=0")
tw2019$PFP=recode(dataset$partyid,"4=1;else=0")
tw2019$NEW=recode(dataset$partyid,"3=1;else=0")
tw2019$NPP=recode(dataset$partyid,"6=1;else=0")
tw2019$TPP=NA

tw2019$year=2019
tw2019$taipei=NA
tw2019$region=dataset$AREAR

summary(tw2019)


#================================2017=======================================#
dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/TNSS2017/PP1797B1.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)

tw2017=NULL
tw2017$ind_even_attack=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2017$ind_no_attack=recode(dataset$Q15,"1=-2;2=-1;3=1;4=2;else=0")
tw2017=data.frame(tw2017)

tw2017$uni_even_diff=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")
tw2017$iuni_no_diff=recode(dataset$Q17,"1=-2;2=-1;3=1;4=2;else=0")


tw2017$chn_att=recode(dataset$Q9,"1=-2;2=-1;3=1;4=2;else=0")
tw2017$usa_def=recode(dataset$Q11,"1=-2;2=-1;3=1;4=2;else=0")
tw2017$ppl_def=recode(dataset$Q22,"1=-2;2=-1;3=1;4=2;else=0")
tw2017$army=recode(dataset$Q10,"1=1;2=-1;else=0")

tw2017$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2017$eco_more=recode(dataset$Q3,"1=1;2=-1;else=0")
tw2017$eco_for_poli=recode(dataset$Q4,"1=-2;2=-1;3=1;4=2;else=0")
tw2017$home_eco_crisis=recode(dataset$Q6,"1=1;2=-1;else=0")
tw2017$tw_eco_crisis=recode(dataset$Q7,"1=1;2=-1;else=0")

tw2017$us_score=NA
tw2017$china_score=NA
tw2017$japan_score=NA

tw2017$tundo6=recode(dataset$Q13,"1=-2;2=2;3=-1;4=1;else=0")

tw2017$TaiwanID=recode(dataset$Q28,"1=1;2=0;3=-1;else=0")
tw2017$age=106-dataset$Q29
tw2017$age[tw2017$age<20]=NA
tw2017$edu=recode(dataset$Q30,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2017$fathershengi=recode(dataset$Q31,"3=1;else=0")
tw2017$male=recode(dataset$Q53,"1=1;else=0")
tw2017$KMT=recode(dataset$partyid,"1=1;else=0")
tw2017$DPP=recode(dataset$partyid,"2=1;else=0")

tw2017$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2017$GREEN=recode(dataset$partyid,"2=1;5=1;6=1;else=0")
tw2017$TSU=recode(dataset$partyid,"5=1;else=0")
tw2017$PFP=recode(dataset$partyid,"4=1;else=0")
tw2017$NEW=recode(dataset$partyid,"3=1;else=0")
tw2017$NPP=recode(dataset$partyid,"6=1;else=0")
tw2017$TPP=NA

tw2017$year=2017
tw2017$taipei=recode(dataset$Q32,"63=1;else=0")
tw2017$region=dataset$AREAR

summary(tw2017)

#============================================================#
#============2016============================================#

dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/PRELIMMMMMM/Patience Protest/Taiwan Case/TNSS2016.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)

tw2016=NULL
tw2016$ind_even_attack=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2016$ind_no_attack=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2016=data.frame(tw2016)

tw2016$uni_even_diff=recode(dataset$Q15,"1=-2;2=-1;3=1;4=2;else=0")
tw2016$iuni_no_diff=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")


tw2016$chn_att=recode(dataset$Q26,"1=-2;2=-1;3=1;4=2;else=0")
tw2016$usa_def=recode(dataset$Q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2016$ppl_def=recode(dataset$Q28,"1=-2;2=-1;3=1;4=2;else=0")
tw2016$army=recode(dataset$Q22,"1=1;2=-1;else=0")

tw2016$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2016$eco_more=recode(dataset$Q3,"1=1;2=-1;else=0")
tw2016$eco_for_poli=recode(dataset$Q4,"1=-2;2=-1;3=1;4=2;else=0")
tw2016$home_eco_crisis=recode(dataset$Q6,"1=1;2=-1;else=0")
tw2016$tw_eco_crisis=recode(dataset$Q7,"1=1;2=-1;else=0")

tw2016$us_score=dataset$Q8
tw2016$us_score[tw2016$us_score>=11]=NA
tw2016$china_score=dataset$Q9
tw2016$china_score[tw2016$china_score>=11]=NA
tw2016$japan_score=dataset$Q10
tw2016$japan_score[tw2016$japan_score>=11]=NA

tw2016$tundo6=recode(dataset$Q12,"1=-2;2=2;3=-1;4=1;else=0")

tw2016$TaiwanID=recode(dataset$Q35,"1=1;2=0;3=-1;else=0")
tw2016$age=105-dataset$Q36
tw2016$age[tw2016$age<20]=NA
tw2016$edu=recode(dataset$Q37,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2016$fathershengi=recode(dataset$Q38,"3=1;else=0")
tw2016$male=recode(dataset$Q63,"1=1;else=0")
tw2016$KMT=recode(dataset$partyid,"1=1;else=0")
tw2016$DPP=recode(dataset$partyid,"2=1;else=0")

tw2016$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2016$GREEN=recode(dataset$partyid,"2=1;5=1;6=1;else=0")
tw2016$TSU=recode(dataset$partyid,"5=1;else=0")
tw2016$PFP=recode(dataset$partyid,"4=1;else=0")
tw2016$NEW=recode(dataset$partyid,"3=1;else=0")
tw2016$NPP=recode(dataset$partyid,"6=1;else=0")
tw2016$TPP=NA

tw2016$year=2016
tw2016$taipei=recode(dataset$Q39,"63=1;else=0")
tw2016$region=dataset$AREAR

summary(tw2016)


#============================================================#
#============2015============================================#

dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/PRELIMMMMMM/Patience Protest/Taiwan Case/TNSS2015.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)

tw2015=NULL
tw2015$ind_even_attack=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2015$ind_no_attack=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2015=data.frame(tw2015)

tw2015$uni_even_diff=recode(dataset$Q15,"1=-2;2=-1;3=1;4=2;else=0")
tw2015$iuni_no_diff=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")


tw2015$chn_att=recode(dataset$Q27,"1=-2;2=-1;3=1;4=2;else=0")
tw2015$usa_def=recode(dataset$Q30,"1=-2;2=-1;3=1;4=2;else=0")
tw2015$ppl_def=recode(dataset$Q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2015$army=recode(dataset$Q22,"1=1;2=-1;else=0")

tw2015$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2015$eco_more=recode(dataset$Q3,"1=1;2=-1;else=0")
tw2015$eco_for_poli=recode(dataset$Q4,"1=-2;2=-1;3=1;4=2;else=0")
tw2015$home_eco_crisis=recode(dataset$Q7,"1=1;2=-1;else=0")
tw2015$tw_eco_crisis=NA

tw2015$us_score=dataset$Q8
tw2015$us_score[tw2015$us_score>=11]=NA
tw2015$china_score=dataset$Q9
tw2015$china_score[tw2015$china_score>=11]=NA
tw2015$japan_score=dataset$Q10
tw2015$japan_score[tw2015$japan_score>=11]=NA


tw2015$tundo6=recode(dataset$Q12,"1=-2;2=2;3=-1;4=1;else=0")

tw2015$TaiwanID=recode(dataset$Q36,"1=1;2=0;3=-1;else=0")
tw2015$age=104-dataset$Q37
tw2015$age[tw2015$age<20]=NA
tw2015$edu=recode(dataset$Q38,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2015$fathershengi=recode(dataset$Q39,"3=1;else=0")
tw2015$male=recode(dataset$Q62,"1=1;else=0")
tw2015$KMT=recode(dataset$partyid,"1=1;else=0")
tw2015$DPP=recode(dataset$partyid,"2=1;else=0")


tw2015$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2015$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2015$TSU=recode(dataset$partyid,"5=1;else=0")
tw2015$PFP=recode(dataset$partyid,"4=1;else=0")
tw2015$NEW=recode(dataset$partyid,"3=1;else=0")
tw2015$NPP=NA
tw2015$TPP=NA

tw2015$year=2015
tw2015$taipei=recode(dataset$Q40,"63=1;else=0")
tw2015$region=dataset$AREAR

summary(tw2015)

#============================2014===========================#
#===========================================================#
dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2014_Public_Opinion_and_Security_in_the_Taiwan_Strait_Survey/PP1497C4.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2014=NULL
tw2014$ind_even_attack=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2014$ind_no_attack=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2014=data.frame(tw2014)

tw2014$uni_even_diff=recode(dataset$Q15,"1=-2;2=-1;3=1;4=2;else=0")
tw2014$iuni_no_diff=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")


tw2014$chn_att=recode(dataset$Q27,"1=-2;2=-1;3=1;4=2;else=0")
tw2014$usa_def=recode(dataset$Q30,"1=-2;2=-1;3=1;4=2;else=0")
tw2014$ppl_def=recode(dataset$Q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2014$army=recode(dataset$Q22,"1=1;2=-1;else=0")

tw2014$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2014$eco_more=recode(dataset$Q5,"1=1;2=-1;else=0")
tw2014$eco_for_poli=recode(dataset$Q6,"1=-2;2=-1;3=1;4=2;else=0")
tw2014$home_eco_crisis=recode(dataset$Q7,"1=1;2=-1;else=0")
tw2014$tw_eco_crisis=NA

tw2014$us_score=dataset$Q8
tw2014$us_score[tw2014$us_score>=11]=NA
tw2014$china_score=dataset$Q9
tw2014$china_score[tw2014$china_score>=11]=NA
tw2014$japan_score=dataset$Q10
tw2014$japan_score[tw2014$japan_score>=11]=NA


tw2014$tundo6=recode(dataset$Q12,"1=-2;2=2;3=-1;4=1;else=0")

tw2014$TaiwanID=recode(dataset$Q36,"1=1;2=0;3=-1;else=0")
tw2014$age=103-dataset$Q37
tw2014$age[tw2014$age<20]=NA
tw2014$edu=recode(dataset$Q38,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2014$fathershengi=recode(dataset$Q39,"3=1;else=0")
tw2014$male=recode(dataset$Q61,"1=1;else=0")
tw2014$KMT=recode(dataset$partyid,"1=1;else=0")
tw2014$DPP=recode(dataset$partyid,"2=1;else=0")

tw2014$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2014$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2014$TSU=recode(dataset$partyid,"5=1;else=0")
tw2014$PFP=recode(dataset$partyid,"4=1;else=0")
tw2014$NEW=recode(dataset$partyid,"3=1;else=0")
tw2014$NPP=NA
tw2014$TPP=NA

tw2014$year=2014
tw2014$taipei=recode(dataset$Q40,"63=1;else=0")
tw2014$region=dataset$arear

summary(tw2014)

#=====================2013=======================#
#================================================#


dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2013/PP1397B1.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2013=NULL
tw2013$ind_even_attack=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2013$ind_no_attack=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2013=data.frame(tw2013)

tw2013$uni_even_diff=recode(dataset$Q15,"1=-2;2=-1;3=1;4=2;else=0")
tw2013$iuni_no_diff=recode(dataset$Q16,"1=-2;2=-1;3=1;4=2;else=0")


tw2013$chn_att=recode(dataset$Q27,"1=-2;2=-1;3=1;4=2;else=0")
tw2013$usa_def=recode(dataset$Q30,"1=-2;2=-1;3=1;4=2;else=0")
tw2013$ppl_def=recode(dataset$Q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2013$army=recode(dataset$Q22,"1=1;2=-1;else=0")

tw2013$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2013$eco_more=recode(dataset$Q7,"1=1;2=-1;else=0")
tw2013$eco_for_poli=recode(dataset$Q6,"1=-2;2=-1;3=1;4=2;else=0")
tw2013$home_eco_crisis=NA
tw2013$tw_eco_crisis=NA

tw2013$us_score=dataset$Q8
tw2013$us_score[tw2013$us_score>=11]=NA
tw2013$china_score=dataset$Q9
tw2013$china_score[tw2013$china_score>=11]=NA
tw2013$japan_score=dataset$Q10
tw2013$japan_score[tw2013$japan_score>=11]=NA


tw2013$tundo6=recode(dataset$Q12,"1=-2;2=2;3=-1;4=1;else=0")

tw2013$TaiwanID=recode(dataset$Q36,"1=1;2=0;3=-1;else=0")
tw2013$age=102-dataset$Q37
tw2013$age[tw2013$age<20]=NA
tw2013$edu=recode(dataset$Q38,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2013$fathershengi=recode(dataset$Q39,"3=1;else=0")
tw2013$male=recode(dataset$Q61,"1=1;else=0")
tw2013$KMT=recode(dataset$partyid,"1=1;else=0")
tw2013$DPP=recode(dataset$partyid,"2=1;else=0")

tw2013$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2013$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2013$TSU=recode(dataset$partyid,"5=1;else=0")
tw2013$PFP=recode(dataset$partyid,"4=1;else=0")
tw2013$NEW=recode(dataset$partyid,"3=1;else=0")
tw2013$NPP=NA
tw2013$TPP=NA

tw2013$year=2013
tw2013$taipei=NA
tw2013$region=dataset$arear

summary(tw2013)

#=====================2012=======================#
#================================================#


dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2012/PP1297A5.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2012=NULL
tw2012$ind_even_attack=recode(dataset$Q12,"1=-2;2=-1;3=1;4=2;else=0")
tw2012$ind_no_attack=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2012=data.frame(tw2012)

tw2012$uni_even_diff=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")
tw2012$iuni_no_diff=recode(dataset$Q15,"1=-2;2=-1;3=1;4=2;else=0")


tw2012$chn_att=recode(dataset$Q27,"1=-2;2=-1;3=1;4=2;else=0")
tw2012$usa_def=recode(dataset$Q30,"1=-2;2=-1;3=1;4=2;else=0")
tw2012$ppl_def=recode(dataset$Q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2012$army=recode(dataset$Q21,"1=1;2=-1;else=0")

tw2012$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2012$eco_more=recode(dataset$Q7,"1=1;2=-1;else=0")
tw2012$eco_for_poli=recode(dataset$Q6,"1=-2;2=-1;3=1;4=2;else=0")
tw2012$home_eco_crisis=NA
tw2012$tw_eco_crisis=NA

tw2012$us_score=dataset$Q8
tw2012$us_score[tw2012$us_score>=11]=NA
tw2012$china_score=dataset$Q9
tw2012$china_score[tw2012$china_score>=11]=NA
tw2012$japan_score=dataset$Q10
tw2012$japan_score[tw2012$japan_score>=11]=NA


tw2012$tundo6=recode(dataset$Q11,"1=-2;2=2;3=-1;4=1;else=0")

tw2012$TaiwanID=recode(dataset$Q36,"1=1;2=0;3=-1;else=0")
tw2012$age=101-dataset$Q37
tw2012$age[tw2012$age<20]=NA
tw2012$edu=recode(dataset$Q38,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2012$fathershengi=recode(dataset$Q39,"3=1;else=0")
tw2012$male=recode(dataset$Q61,"1=1;else=0")
tw2012$KMT=recode(dataset$partyid,"1=1;else=0")
tw2012$DPP=recode(dataset$partyid,"2=1;else=0")

tw2012$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2012$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2012$TSU=recode(dataset$partyid,"5=1;else=0")
tw2012$PFP=recode(dataset$partyid,"4=1;else=0")
tw2012$NEW=recode(dataset$partyid,"3=1;else=0")
tw2012$NPP=NA
tw2012$TPP=NA

tw2012$year=2012
tw2012$taipei=recode(dataset$Q40,"63=1;else=0")
tw2012$region=dataset$arear

summary(tw2012)

#========================2011=======================#
#===================================================#


dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2011/PP119721.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2011=NULL
tw2011$ind_even_attack=recode(dataset$Q11,"1=-2;2=-1;3=1;4=2;else=0")
tw2011$ind_no_attack=recode(dataset$Q12,"1=-2;2=-1;3=1;4=2;else=0")
tw2011=data.frame(tw2011)

tw2011$uni_even_diff=recode(dataset$Q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2011$iuni_no_diff=recode(dataset$Q14,"1=-2;2=-1;3=1;4=2;else=0")


tw2011$chn_att=recode(dataset$Q28,"1=-2;2=-1;3=1;4=2;else=0")
tw2011$usa_def=recode(dataset$Q31,"1=-2;2=-1;3=1;4=2;else=0")
tw2011$ppl_def=recode(dataset$Q30,"1=-2;2=1;3=2;else=-1")
tw2011$army=recode(dataset$Q19,"1=1;2=-1;else=0")

tw2011$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2011$eco_more=recode(dataset$Q7,"1=1;2=-1;else=0")
tw2011$eco_for_poli=recode(dataset$Q7,"1=-2;2=2;else=0")
tw2011$home_eco_crisis=NA
tw2011$tw_eco_crisis=NA

tw2011$us_score=dataset$Q8
tw2011$us_score[tw2011$us_score>=11]=NA
tw2011$china_score=dataset$Q9
tw2011$china_score[tw2011$china_score>=11]=NA
tw2011$japan_score=NA


tw2011$tundo6=recode(dataset$Q10,"1=-2;2=2;3=-1;4=1;else=0")

tw2011$TaiwanID=recode(dataset$Q38,"1=1;2=0;3=-1;else=0")
tw2011$age=100-dataset$Q39
tw2011$age[tw2011$age<20]=NA
tw2011$edu=recode(dataset$Q40,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2011$fathershengi=recode(dataset$Q41,"3=1;else=0")
tw2011$male=recode(dataset$Q66,"1=1;else=0")
tw2011$KMT=recode(dataset$partyid,"1=1;else=0")
tw2011$DPP=recode(dataset$partyid,"2=1;else=0")

tw2011$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2011$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2011$TSU=recode(dataset$partyid,"5=1;else=0")
tw2011$PFP=recode(dataset$partyid,"4=1;else=0")
tw2011$NEW=recode(dataset$partyid,"3=1;else=0")
tw2011$NPP=NA
tw2011$TPP=NA

tw2011$year=2011
tw2011$taipei=recode(dataset$Q45,"63=1;else=0")
tw2011$region=dataset$arear

summary(tw2011)


#==================2008==========================#
#================================================#


dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2008/PP0821.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2008=NULL
tw2008$ind_even_attack=recode(dataset$q7,"1=-2;2=-1;3=1;4=2;else=0")
tw2008$ind_no_attack=recode(dataset$q8,"1=-2;2=-1;3=1;4=2;else=0")
tw2008=data.frame(tw2008)

tw2008$uni_even_diff=recode(dataset$q9,"1=-2;2=-1;3=1;4=2;else=0")
tw2008$iuni_no_diff=recode(dataset$q10,"1=-2;2=-1;3=1;4=2;else=0")


tw2008$chn_att=recode(dataset$q28,"1=-2;2=-1;3=1;4=2;else=0")
tw2008$usa_def=recode(dataset$q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2008$ppl_def=recode(dataset$q17,"1=-2;2=1;3=2;else=-1")
tw2008$army=recode(dataset$q15,"1=1;2=-1;else=0")

tw2008$business=recode(dataset$q2,"1=1;2=-1;else=0")
tw2008$eco_more=recode(dataset$q3,"1=1;2=-1;else=0")
tw2008$eco_for_poli=recode(dataset$q3,"1=-2;2=2;else=0")
tw2008$home_eco_crisis=NA
tw2008$tw_eco_crisis=NA

tw2008$us_score=dataset$q5
tw2008$us_score[tw2008$us_score>=11]=NA
tw2008$china_score=dataset$q4
tw2008$china_score[tw2008$china_score>=11]=NA
tw2008$japan_score=NA


tw2008$tundo6=recode(dataset$q6,"1=-2;2=2;3=-1;4=1;else=0")

tw2008$TaiwanID=recode(dataset$q38,"1=1;2=0;3=-1;else=0")
tw2008$age=97-dataset$q39
tw2008$age[tw2008$age<20]=NA
tw2008$edu=recode(dataset$q40,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2008$fathershengi=recode(dataset$q41,"3=1;else=0")
tw2008$male=recode(dataset$q70,"1=1;else=0")
tw2008$KMT=recode(dataset$partyid,"1=1;else=0")
tw2008$DPP=recode(dataset$partyid,"2=1;else=0")


tw2008$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2008$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2008$TSU=recode(dataset$partyid,"5=1;else=0")
tw2008$PFP=recode(dataset$partyid,"4=1;else=0")
tw2008$NEW=recode(dataset$partyid,"3=1;else=0")
tw2008$NPP=NA
tw2008$TPP=NA

tw2008$year=2008
tw2008$taipei=recode(dataset$q46,"63=1;else=0")
tw2008$region=dataset$arear
summary(tw2008)

#==================2005==========================#
#================================================#


dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2005/pp059752.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2005=NULL
tw2005$ind_even_attack=recode(dataset$Q7,"1=-2;2=-1;3=1;4=2;else=0")
tw2005$ind_no_attack=recode(dataset$Q8,"1=-2;2=-1;3=1;4=2;else=0")
tw2005=data.frame(tw2005)

tw2005$uni_even_diff=recode(dataset$Q9,"1=-2;2=-1;3=1;4=2;else=0")
tw2005$iuni_no_diff=recode(dataset$Q10,"1=-2;2=-1;3=1;4=2;else=0")


tw2005$chn_att=recode(dataset$Q29,"1=-2;2=-1;3=1;4=2;else=0")
tw2005$usa_def=recode(dataset$Q30,"1=-2;2=-1;3=1;4=2;else=0")
tw2005$ppl_def=recode(dataset$Q15,"1=-2;2=1;3=2;else=-1")
tw2005$army=recode(dataset$Q13,"1=1;2=-1;else=0")

tw2005$business=recode(dataset$Q2,"1=1;2=-1;else=0")
tw2005$eco_more=recode(dataset$Q3,"1=1;2=-1;else=0")
tw2005$eco_for_poli=recode(dataset$Q3,"1=-2;2=2;else=0")
tw2005$home_eco_crisis=NA
tw2005$tw_eco_crisis=NA

tw2005$us_score=dataset$Q5
tw2005$us_score[tw2005$us_score>=11]=NA
tw2005$china_score=dataset$Q4
tw2005$china_score[tw2005$china_score>=11]=NA
tw2005$japan_score=NA


tw2005$tundo6=recode(dataset$Q6,"1=-2;2=2;3=-1;4=1;else=0")

tw2005$TaiwanID=recode(dataset$Q38,"1=1;2=0;3=-1;else=0")
tw2005$age=94-dataset$Q39
tw2005$age[tw2005$age<20]=NA
tw2005$edu=recode(dataset$Q41,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2005$fathershengi=recode(dataset$Q40,"3=1;else=0")
tw2005$male=recode(dataset$Q68,"1=1;else=0")
tw2005$KMT=recode(dataset$Q34,"1=1;else=0")
tw2005$KMT[dataset$Q36==1]=1
tw2005$DPP=recode(dataset$Q34,"2=1;else=0")
tw2005$DPP[dataset$Q36==2]=1

tw2005$BLUE=recode(dataset$Q34,"1=1;3=1;4=1;else=0")
tw2005$BLUE[dataset$Q36==1]=1
tw2005$BLUE[dataset$Q36==3]=1
tw2005$BLUE[dataset$Q36==4]=1

tw2005$GREEN=recode(dataset$Q34,"2=1;5=1;else=0")
tw2005$GREEN[dataset$Q36==2]=1
tw2005$GREEN[dataset$Q36==5]=1
tw2005$TSU=recode(dataset$Q34,"5=1;else=0")
tw2005$TSU[dataset$Q36==3]=1

tw2005$PFP=recode(dataset$Q34,"4=1;else=0")
tw2005$PFP[dataset$Q36==4]=1

tw2005$NEW=recode(dataset$Q34,"3=1;else=0")
tw2005$NEW[dataset$Q36==3]=1

tw2005$NPP=NA
tw2005$TPP=NA


tw2005$year=2005
tw2005$taipei=recode(dataset$Q43,"63=1;else=0")
tw2005$region=NA

summary(tw2005)

#===================2004===========================#
#==================================================#

dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2004/PT049750.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2004=NULL
tw2004$ind_even_attack=recode(dataset$q7,"1=-2;2=-1;3=1;4=2;else=0")
tw2004$ind_no_attack=recode(dataset$q8,"1=-2;2=-1;3=1;4=2;else=0")
tw2004=data.frame(tw2004)

tw2004$uni_even_diff=recode(dataset$q9,"1=-2;2=-1;3=1;4=2;else=0")
tw2004$iuni_no_diff=recode(dataset$q10,"1=-2;2=-1;3=1;4=2;else=0")


tw2004$chn_att=recode(dataset$q12,"1=-2;2=-1;3=1;4=2;else=0")
tw2004$usa_def=recode(dataset$q13,"1=-2;2=-1;3=1;4=2;else=0")
tw2004$ppl_def=recode(dataset$q19,"1=-2;2=1;3=2;else=-1")
tw2004$army=recode(dataset$q17,"1=1;2=-1;else=0")

tw2004$business=recode(dataset$q2,"1=1;2=-1;else=0")
tw2004$eco_more=recode(dataset$q5,"1=1;2=-1;else=0")
tw2004$eco_for_poli=recode(dataset$q5,"1=-2;2=2;else=0")
tw2004$home_eco_crisis=NA
tw2004$tw_eco_crisis=NA

tw2004$us_score=dataset$q21
tw2004$us_score[tw2004$us_score>=11]=NA
tw2004$china_score=NA
tw2004$japan_score=NA


tw2004$tundo6=recode(dataset$q6,"1=-2;2=2;3=-1;4=1;else=0")

tw2004$TaiwanID=recode(dataset$q35,"1=1;2=0;3=-1;else=0")
tw2004$age=93-dataset$q36
tw2004$age[tw2004$age<20]=NA
tw2004$edu=recode(dataset$q38,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2004$fathershengi=recode(dataset$q37,"3=1;else=0")
tw2004$male=recode(dataset$q65,"1=1;else=0")
tw2004$KMT=recode(dataset$partyid,"1=1;else=0")
tw2004$DPP=recode(dataset$partyid,"2=1;else=0")

tw2004$BLUE=recode(dataset$partyid,"1=1;3=1;4=1;else=0")
tw2004$GREEN=recode(dataset$partyid,"2=1;5=1;else=0")
tw2004$TSU=recode(dataset$partyid,"5=1;else=0")
tw2004$PFP=recode(dataset$partyid,"4=1;else=0")
tw2004$NEW=recode(dataset$partyid,"3=1;else=0")
tw2004$NPP=NA
tw2004$TPP=NA

tw2004$year=2004
tw2004$taipei=recode(dataset$q40,"63=1;else=0")
tw2004$region=dataset$arear

summary(tw2004)

#===================2002===========================#
#==================================================#

dataset = read.spss("C:/Users/weary/Dropbox/Duke Prepare/2/national security survey/2003/pt0297c4.sav", 
                    to.data.frame=TRUE,use.value.labels=FALSE)


tw2002=NULL
tw2002$ind_even_attack=recode(dataset$q10,"1=-2;2=-1;3=1;4=2;else=0")
tw2002$ind_no_attack=recode(dataset$q11,"1=-2;2=-1;3=1;4=2;else=0")
tw2002=data.frame(tw2002)

tw2002$uni_even_diff=recode(dataset$q12,"1=-2;2=-1;3=1;4=2;else=0")
tw2002$iuni_no_diff=recode(dataset$q13,"1=-2;2=-1;3=1;4=2;else=0")


tw2002$chn_att=recode(dataset$q16,"1=-2;2=-1;3=1;4=2;else=0")
tw2002$usa_def=recode(dataset$q17,"1=-2;2=-1;3=1;4=2;else=0")
tw2002$ppl_def=recode(dataset$q27,"1=-1;2=-2;3=1;4=2;else=0")
tw2002$army=NA

tw2002$business=recode(dataset$q2,"1=1;2=-1;else=0")
tw2002$eco_more=recode(dataset$q5,"1=2;2=1;3=-1;4=-2;else=0")
tw2002$eco_for_poli=recode(dataset$q7,"1=-2;2=-1;3=1;4=2;else=0")
tw2002$home_eco_crisis=NA
tw2002$tw_eco_crisis=NA

tw2002$us_score=NA
tw2002$china_score=NA
tw2002$japan_score=NA


tw2002$tundo6=recode(dataset$q9,"1=-2;2=2;3=-1;4=1;else=0")

tw2002$TaiwanID=recode(dataset$q34,"1=1;2=0;3=-1;else=0")
tw2002$age=91-dataset$q35
tw2002$age[tw2002$age<20]=NA
tw2002$edu=recode(dataset$q37,"1=1;2=2;3=3;4=4;5=5;6=6;7=7;else=NA")
tw2002$fathershengi=recode(dataset$q36,"3=1;else=0")
tw2002$male=recode(dataset$q66,"1=1;else=0")

tw2002$KMT=recode(dataset$q31,"1=1;else=0")
tw2002$KMT[dataset$q33==1]=1
tw2002$DPP=recode(dataset$q31,"2=1;else=0")
tw2002$DPP[dataset$q33==2]=1


tw2002$BLUE=recode(dataset$q31,"1=1;3=1;4=1;else=0")
tw2002$BLUE[dataset$q33==1]=1
tw2002$BLUE[dataset$q33==3]=1
tw2002$BLUE[dataset$q33==4]=1

tw2002$GREEN=recode(dataset$q31,"2=1;5=1;else=0")
tw2002$GREEN[dataset$q33==2]=1
tw2002$GREEN[dataset$q33==5]=1
tw2002$TSU=recode(dataset$q31,"5=1;else=0")
tw2002$TSU[dataset$q33==3]=1

tw2002$PFP=recode(dataset$q31,"4=1;else=0")
tw2002$PFP[dataset$q33==4]=1

tw2002$NEW=recode(dataset$q31,"3=1;else=0")
tw2002$NEW[dataset$q33==3]=1

tw2002$NPP=NA
tw2002$TPP=NA

tw2002$year=2002

tw2002$taipei=recode(dataset$q41,"63=1;else=0")
tw2002$region=dataset$arear


summary(tw2002)

#====================ANALYSIS!!!!!!!!!!!!!!======================#

tw_all=rbind(tw2002,tw2004,tw2005,tw2008,tw2011,tw2012,tw2013,tw2014,tw2015,tw2016,tw2017,tw2019,tw2020,tw2022)
head(tw_all)
summary(tw_all)

tw_all$TaiwanID2=0
tw_all$TaiwanID2[tw_all$TaiwanID==1]=1


#======Figure 3 and Table 3==========#
tw_all$ind_even_attack2=0
tw_all$ind_even_attack2[tw_all$ind_even_attack>0]=1
tw_all$ind_no_attack2=0
tw_all$ind_no_attack2[tw_all$ind_no_attack>0]=1
tw_all$uni_even_diff2=0
tw_all$uni_even_diff2[tw_all$uni_even_diff>0]=1
tw_all$iuni_no_diff2=0
tw_all$iuni_no_diff2[tw_all$iuni_no_diff>0]=1

sumy=summarySE(tw_all, measurevar="ind_even_attack2", groupvars=c("year"))
sumy
sumy2=summarySE(tw_all, measurevar="ind_no_attack2", groupvars=c("year"))
sumy2
sumy3=summarySE(tw_all, measurevar="uni_even_diff2", groupvars=c("year"))
sumy3
sumy4=summarySE(tw_all, measurevar="iuni_no_diff2", groupvars=c("year"))
sumy4

sumy5=NULL
sumy5$group=c(rep("Ind even attack",14),
              rep("Ind if no attack",14),
              rep("Uni even diff",14),
              rep("Uni if no diff",14))
sumy5$opinion=NA
sumy5$opinion[c(1:14)]=sumy$ind_even_attack2
sumy5$opinion[c(15:28)]=sumy2$ind_no_attack2
sumy5$opinion[c(29:42)]=sumy3$uni_even_diff2
sumy5$opinion[c(43:56)]=sumy4$iuni_no_diff2
sumy5=data.frame(sumy5)

sumy5$year=rep(c(sumy$year),4)

sumy5
sumy5$group=
  factor(sumy5$group, levels = c("Ind if no attack", "Ind even attack", 
                                 "Uni if no diff","Uni even diff"))

library(ggplot2)

ggplot(data=sumy5,aes(x=year,y=opinion,shape=group,colour=group))+
  geom_point(size=3)+
  theme_bw()+geom_line(size=1)+
  scale_x_continuous(breaks=sumy5$year,limits=c(2001,2023))+
  scale_shape_manual(values=c(15,16,17,18))+
  scale_colour_manual(values=c("gray50","green2","blue","red"))+
  xlab("Source: TNSS2002-2022 (n=16494)")+ylab("% Support the statement")+
  ylim(0,0.8)+
  theme(axis.text.x = element_text(size=7,angle=-90),
        axis.text.y = element_text(size=7),
        legend.title = element_blank())+
  geom_text(data = NULL, x = 2000.9, y = 0.555, label = "56%",colour="blue",size=2.5)+
  geom_text(data = NULL, x = 2023.1, y = 0.268,label = "27%",colour="blue",size=2.5)+
  geom_text(data = NULL, x = 2000.9, y = 0.208, label = "21%",colour="red",size=2.5)+
  geom_text(data = NULL, x = 2023.1, y = 0.134,label = "13%",colour="red",size=2.5)+
  geom_text(data = NULL, x = 2000.9, y = 0.646, label = "65%",colour="gray50",size=2.5)+
  geom_text(data = NULL, x = 2023.1, y = 0.717,label = "72%",colour="gray50",size=2.5)+
  geom_text(data = NULL, x = 2000.9, y = 0.227, label = "23%",colour="green2",size=2.5)+
  geom_text(data = NULL, x = 2023.1, y = 0.359,label = "36%",colour="green2",size=2.5)


sumy5$N=rep(c(sumy$N),4)
sumy5$ron=round(sumy5$opinion,3)

sumy6=sumy
sumy6$ind_even_attack2=round(sumy$ind_even_attack2,3)
sumy6$ind_no_attack2=round(sumy2$ind_no_attack2,3)
sumy6$uni_even_diff2=round(sumy3$uni_even_diff2,3)
sumy6$iuni_no_diff2=round(sumy4$iuni_no_diff2,3)
sumy6=sumy6[,c(-4,-5,-6)]
sumy6
sumy6=data.frame(sumy6)

#=================Traditional Tondu======================#
#=================Figure 2 and Table 2======================#

table(tw_all$year,tw_all$tundo6)

prop.table(table(tw_all$year,tw_all$tundo6),1)
tundo=data.frame(prop.table(table(tw_all$year,tw_all$tundo6),1))

tundo$group=NA
tundo$group[tundo$Var2=="-2"]="Uni soon"
tundo$group[tundo$Var2=="-1"]="Uni later"
tundo$group[tundo$Var2=="0"]="Status Quo"
tundo$group[tundo$Var2=="1"]="Ind later"
tundo$group[tundo$Var2=="2"]="Ind soon"
table(tundo$group)
tundo$Var1=as.numeric(as.character(tundo$Var1))

write.csv(tundo,"tundo.csv")



tundo$group[tundo$Var2=="-2"]="Uni soon"
tundo$group[tundo$Var2=="-1"]="Uni later"
tundo$group[tundo$Var2=="0"]="Status Quo"
tundo$group[tundo$Var2=="1"]="Ind later"
tundo$group[tundo$Var2=="2"]="Ind soon"

tundo$group=
  factor(tundo$group, levels = c("Ind soon", "Ind later",
                                 "Status Quo",
                                 "Uni later","Uni soon"))


ggplot(tundo, aes(fill=group, y=Freq, x=Var1)) + 
  geom_bar(position="fill", stat="identity")+
  scale_x_continuous(breaks=tundo$Var1,limits=c(2001,2023))+
  theme_bw()+
  scale_fill_manual(values=c("green2","yellow","gray50","blue","red"))+
  xlab("Source: TNSS2002-2022 (n=16494)")+ylab("% of Respondents")+
  theme(axis.text.x = element_text(size=7,angle=-90),
        axis.text.y = element_text(size=7),
        legend.title = element_blank())+
  geom_text(data = NULL, x = 2000.9, y = 0.13, label = "22%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.05,label = "6.6%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2000.9, y = 0.019, label = "2.9%",colour="red",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.014,label = "1.4%",colour="red",size=2)+
  geom_text(data = NULL, x = 2000.9, y = 0.56, label = "60%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.42,label = "63%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2000.9, y = 0.99, label = "3.0%",colour="green2",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.97,label = "5.8%",colour="green2",size=2)+
  geom_text(data = NULL, x = 2000.9, y = 0.91, label = "12%",colour="yellow",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.82,label = "23%",colour="yellow",size=2)


#=================Change in Identity======================#
#===========Table 1 and Figure 1==========================#
table(tw_all$year,tw_all$TaiwanID)

prop.table(table(tw_all$year,tw_all$TaiwanID),1)
TID=data.frame(prop.table(table(tw_all$year,tw_all$TaiwanID),1))

TID$group=NA
TID$group[TID$Var2=="-1"]="Chinese only"
TID$group[TID$Var2=="0"]="Both"
TID$group[TID$Var2=="1"]="Taiwanese only"
table(TID$group)
TID$Var1=as.numeric(as.character(TID$Var1))

TID$group=
  factor(TID$group, levels = c("Chinese only", "Both",
                                 "Taiwanese only"))



ggplot(TID, aes(fill=group, y=Freq, x=Var1)) + 
  geom_bar(position="fill", stat="identity")+
  scale_x_continuous(breaks=TID$Var1,limits=c(2001,2023))+
  theme_bw()+
  scale_fill_manual(values=c("blue","gray50","green2"))+
  xlab("Source: TNSS2002-2022 (n=16494)")+ylab("% of Respondents")+
  theme(axis.text.x = element_text(size=7,angle=-90),
        axis.text.y = element_text(size=7),
        legend.title = element_blank())+
  geom_text(data = NULL, x = 2000.9, y = 0.95, label = "9%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.98,label = "4%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2000.9, y = 0.62, label = "59%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.78,label = "36%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2000.9, y = 0.16, label = "32%",colour="green2",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.3,label = "60%",colour="green2",size=2)

#test four calculation================================================================
#=============Table 5 and Figure 5=========================#
table(tw_all$usa_def,tw_all$year)
table(tw_all$chn_att,tw_all$year)
table(tw_all$ppl_def,tw_all$year)
table(tw_all$army,tw_all$year)

tw_all$usadef2=0
tw_all$usadef2[tw_all$usa_def>0]=1
tw_all$chn_att2=0
tw_all$chn_att2[tw_all$chn_att>0]=1
tw_all$ppl_def2=0
tw_all$ppl_def2[tw_all$ppl_def>0]=1
tw_all$army2=0
tw_all$army2[tw_all$army>0]=1

sumy=summarySE(tw_all, measurevar="usadef2", groupvars=c("year"))
sumy
sumy2=summarySE(tw_all, measurevar="chn_att2", groupvars=c("year"))
sumy2
sumy3=summarySE(tw_all, measurevar="ppl_def2", groupvars=c("year"))
sumy3
sumy4=summarySE(tw_all, measurevar="army2", groupvars=c("year"))
sumy4

sumy5=NULL
sumy5$group=c(rep("US will help defend TW",14),
              rep("After TW Ind, CN will attack",14),
              rep("After CN attack, TW ppl will resist",14),
              rep("TW army capable of defense",14))
sumy5$opinion=NA
sumy5$opinion[c(1:14)]=sumy$usadef2
sumy5$opinion[c(15:28)]=sumy2$chn_att2
sumy5$opinion[c(29:42)]=sumy3$ppl_def2
sumy5$opinion[c(43:56)]=sumy4$army2
sumy5=data.frame(sumy5)

sumy5$year=rep(c(sumy$year),4)

sumy5
sumy5=sumy5[c(-14,-43),]
sumy5
sumy5$group=
  factor(sumy5$group, levels = c("After CN attack, TW ppl will resist",
                                 "After TW Ind, CN will attack", 
                                 "US will help defend TW","TW army capable of defense"))

ggplot(data=sumy5,aes(x=year,y=opinion,shape=group,colour=group))+
  geom_point(size=3)+
  theme_bw()+geom_line(size=1)+
  scale_x_continuous(breaks=sumy5$year,limits=c(2001,2023))+
  scale_shape_manual(values=c(15,16,17,18))+
  scale_colour_manual(values=c("gray50","green2","blue","red"))+
  xlab("Source: TNSS2002-2022 (n=16494)")+ylab("% Support the statement")+
  ylim(0,0.8)+
  theme(axis.text.x = element_text(size=7,angle=-90),
        axis.text.y = element_text(size=7),
        legend.title = element_blank())+
  geom_text(data = NULL, x = 2002.8, y = 0.195, label = "20%",colour="red",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.263,label = "26%",colour="red",size=2)+
  geom_text(data = NULL, x = 2000.8, y = 0.472, label = "47%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2021.2, y = 0.514,label = "51%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2000.8, y = 0.673, label = "66%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.639,label = "64%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2000.8, y = 0.652, label = "66%",colour="green2",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.682,label = "68%",colour="green2",size=2)


sumy6=sumy
sumy6$usadef2=round(sumy$usadef2,3)
sumy6$chn_att2=round(sumy2$chn_att2,3)
sumy6$ppl_def2=round(sumy3$ppl_def2,3)
sumy6$army2=round(sumy4$army2,3)
sumy6=sumy6[,c(-4,-5,-6)]
sumy6
sumy6=data.frame(sumy6)




#=============Table 4 and Figure 4=========================#

table(tw_all$eco_more,tw_all$year)
table(tw_all$eco_for_poli,tw_all$year)


tw_all$eco2=0
tw_all$eco2[tw_all$eco_more>0]=1
tw_all$ecopoli2=0
tw_all$ecopoli2[tw_all$eco_for_poli>0]=1
tw_all$ecostill=0
tw_all$ecostill[tw_all$eco_more>0&tw_all$eco_for_poli>0]=1


sumy=summarySE(tw_all, measurevar="eco2", groupvars=c("year"))
sumy
sumy2=summarySE(tw_all, measurevar="ecopoli2", groupvars=c("year"))
sumy2
sumy3=summarySE(tw_all, measurevar="ecostill", groupvars=c("year"))
sumy3

sumy5=NULL
sumy5$group=c(rep("Want more economy with CN",14),
              rep("Worry CN econ coertion for poli gain",14),
              rep("(More eco with CN) AND (worry coertion)",14))
sumy5$opinion=NA
sumy5$opinion[c(1:14)]=sumy$eco2
sumy5$opinion[c(15:28)]=sumy2$ecopoli2
sumy5$opinion[c(29:42)]=sumy3$ecostill
sumy5=data.frame(sumy5)

sumy5$year=rep(c(sumy$year),3)

sumy5
sumy5=sumy5[sumy5$year>=2012,]
sumy5
sumy5$group=
  factor(sumy5$group, levels = c("Want more economy with CN",
                                 "Worry CN econ coertion for poli gain", 
                                 "(More eco with CN) AND (worry coertion)"))


sumy=summarySE(tw_all, measurevar="eco2", groupvars=c("year"))
sumy
sumy2=summarySE(tw_all, measurevar="ecopoli2", groupvars=c("year"))
sumy2
sumy3=summarySE(tw_all, measurevar="ecostill", groupvars=c("year"))
sumy3

sumy5=NULL
sumy5$group=c(rep("Want more economy with CN",14),
              rep("Worry CN econ coertion for poli gain",14),
              rep("(More eco with CN) AND (worry coertion)",14))
sumy5$opinion=NA
sumy5$opinion[c(1:14)]=sumy$eco2
sumy5$opinion[c(15:28)]=sumy2$ecopoli2
sumy5$opinion[c(29:42)]=sumy3$ecostill
sumy5=data.frame(sumy5)

sumy5$year=rep(c(sumy$year),3)

sumy5
sumy5=sumy5[c(-1,-15,-16,-17,-18,-19,
              -29,-30,-31,-32,-33),]
sumy5
sumy5$group=
  factor(sumy5$group, levels = c("Want more economy with CN",
                                 "Worry CN econ coertion for poli gain", 
                                 "(More eco with CN) AND (worry coertion)"))



ggplot(data=sumy5,aes(x=year,y=opinion,shape=group,colour=group))+
  geom_point(size=3)+
  theme_bw()+geom_line(size=1)+
  scale_x_continuous(breaks=sumy5$year,limits=c(2003,2023))+
  scale_shape_manual(values=c(15,16,17))+
  scale_colour_manual(values=c("blue","green2","gray50"))+
  xlab("Source: TNSS2003-2022 (n=15269)")+ylab("% Support the statement")+
  ylim(0,0.8)+
  theme(axis.text.x = element_text(size=7,angle=-90),
        axis.text.y = element_text(size=7),
        legend.title = element_blank())+
  geom_text(data = NULL, x = 2002.8, y = 0.548, label = "55%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.437,label = "44%",colour="blue",size=2)+
  geom_text(data = NULL, x = 2010.8, y = 0.238, label = "24%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.180,label = "18%",colour="gray50",size=2)+
  geom_text(data = NULL, x = 2010.8, y = 0.621, label = "62%",colour="green2",size=2)+
  geom_text(data = NULL, x = 2023.2, y = 0.583,label = "58%",colour="green2",size=2)


sumy6=sumy
sumy6$eco2=round(sumy$eco2,3)
sumy6$ecopoli2=round(sumy2$ecopoli2,3)
sumy6$ecostill=round(sumy3$ecostill,3)
sumy6=sumy6[,c(-4,-5,-6)]
sumy6
sumy6=data.frame(sumy6)
